<?
class ModPbnyWeeklyLeadersHelper
{
	function getDivisions($season){
		
		$result = array();
		$season_division = JModel::getInstance('seasondivision', 'seasondivisionmodel');
		$division        = JModel::getInstance('division', 'divisionmodel');
		
		$sds             = $season_division->get_by_season_id($season->id);
		if($sds)
			foreach($sds as $sd)
				$result[$sd->division_id] = $division->get($sd->division_id);
		
		return $result;
	}
	
	//returns only ids
	function getConferences($division){
		
		$result = array();
		$division_conference = JModel::getInstance('divisionconference', 'divisionconferencemodel');
		$conference          = JModel::getInstance('conference', 'conferencemodel');
		
		$dcs                 = $division_conference->get_by_division_id($division->id);
		if($dcs)
			foreach($dcs as $dc)
				$result[$dc->conference_id] = $conference->get($dc->conference_id);
		
		return $result;
	}
	
    function getTeams($conferences){
		$result = array();
		$teams  = array();
		
		$conference_team = JModel::getInstance( 'conferenceteam', 'conferenceteammodel' );
		$team            = JModel::getInstance( 'team', 'teammodel' );
		
		if($conferences)
			foreach(array_keys($conferences) as $cid)
				$teams = array_merge($teams, $conference_team->get_teams_by_conference_id($cid));	
		
		if($teams)
			foreach($teams as $team)
				$result[$team->id] = $team;
		
		return $result;
    }
	
	function getWeek($timestamp, $season){
	
		$result      = new stdClass;
		if(!$season)
			return $result;
		$week_number = floor(($timestamp - strtotime($season->start_date))/ (60 * 60 * 24 * 7)) ;
		$start       = strtotime($season->start_date) + (60 * 60 * 24 * 7 * $week_number);
		$end         = strtotime($season->start_date) + (60 * 60 * 24 * 7 * ($week_number + 1));
		$result->start_date = strftime('%Y-%m-%d', $start);
		$result->end_date = strftime('%Y-%m-%d', $end);
		return $result;
	}
	
	function getSchedules($conferences, $start_date, $end_date){
		
		$result    = array();
		$schedule  = JModel::getInstance('schedule', 'schedulemodel');
		$schedules = $schedule->get_by_schedule($start_date, $end_date);
		$schedules = $schedule->get(); #schedules temporary code only
		if($schedules){
			foreach($schedules as $s){
				if(array_key_exists($s->conference_id, $conferences))
					$result[$s->id] = $s;
			}
		}
		
		return $result;	
	}
	
	function getStats($schedules){
	
		$result    = array();
		$pgs       = JModel::getInstance('playergamestat', 'playergamestatmodel');
		$mplayer   = JModel::getInstance( 'player', 'playermodel' );
		$sched_ids = array_keys($schedules);
		foreach($sched_ids as $id){
			$stats = $pgs->get_by_schedule_id($id);
			if($stats){
				foreach($stats as $stat){
					
					# check if player exist; other wise dont add to result
					if($mplayer->exist($stat->player_id))
					  $result[$stat->id] = $stat;
				}
			}
		}
		return $result;
	}
	
	function getTopPoints($stats){
	
		$result = array();
		foreach($stats as $stat){
			if(!isset($result[$stat->player_id]))
				$result[$stat->player_id] = array('points' => 0, 'gp' => 0 );
			$result[$stat->player_id]['gp'] += 1;
			$result[$stat->player_id]['points'] += $stat->points;
		}
		
		$result = ModPbnyWeeklyLeadersHelper::subval_sort($result, 'points');
		$count=0;
		foreach($result as $pid => $data){
		  $final_result[$pid] = $data;
		  $count++;
		  if($count>2)
		    break;
		}
		return $final_result;
	}
	
	function getTopRebounds($stats){
	
		$result = array();
		foreach($stats as $stat){
			if(!isset($result[$stat->player_id]))
				$result[$stat->player_id] = array('rebounds' => 0, 'gp' => 0 );
			$result[$stat->player_id]['gp'] += 1;
			$result[$stat->player_id]['rebounds'] += ($stat->def_rebounds + $stat->off_rebounds);
		}
		
		$result = ModPbnyWeeklyLeadersHelper::subval_sort($result, 'rebounds');
		$count=0;
		foreach($result as $pid => $data){
		  $final_result[$pid] = $data;
		  $count++;
		  if($count>2)
		    break;
		}
		return $final_result;
	}
	
	function getTopAssists($stats){
	
		$result = array();
		foreach($stats as $stat){
			if(!isset($result[$stat->player_id]))
				$result[$stat->player_id] = array('assists' => 0, 'gp' => 0 );
			$result[$stat->player_id]['gp'] += 1;
			$result[$stat->player_id]['assists'] += $stat->assists;
		}
		
		$result = ModPbnyWeeklyLeadersHelper::subval_sort($result, 'assists');
		$count=0;
		foreach($result as $pid => $data){
		  $final_result[$pid] = $data;
		  $count++;
		  if($count>2)
		    break;
		}
		return $final_result;
	}
	
	function getTopSteals($stats){
	
		$result = array();
		foreach($stats as $stat){
			if(!isset($result[$stat->player_id]))
				$result[$stat->player_id] = array('steals' => 0, 'gp' => 0 );
			$result[$stat->player_id]['gp'] += 1;
			$result[$stat->player_id]['steals'] += $stat->steals;
		}
		
		$result = ModPbnyWeeklyLeadersHelper::subval_sort($result, 'steals');
		$count=0;
		foreach($result as $pid => $data){
		  $final_result[$pid] = $data;
		  $count++;
		  if($count>2)
		    break;
		}
		return $final_result;
	}
	
	function getTopBlocks($stats){
	
		$result = array();
		foreach($stats as $stat){
			if(!isset($result[$stat->player_id]))
				$result[$stat->player_id] = array('blocks' => 0, 'gp' => 0 );
			$result[$stat->player_id]['gp'] += 1;
			$result[$stat->player_id]['blocks'] += $stat->blocks;
		}
		
		$result = ModPbnyWeeklyLeadersHelper::subval_sort($result, 'blocks');
		$count=0;
		foreach($result as $pid => $data){
		  $final_result[$pid] = $data;
		  $count++;
		  if($count>2)
		    break;
		}
		return $final_result;
	}
	
	function getPlayerDetails($points,$rebounds,$assists,$steals,$blocks){
		
		$result = array();
		
		$mplayer   = JModel::getInstance( 'player', 'playermodel' );
		$mperson   = JModel::getInstance( 'person', 'personmodel' );
		$mpic      = JModel::getInstance( 'pic', 'picmodel' );
		
		$division_leaders = array($points, $rebounds, $assists, $steals, $blocks);
		
		foreach($division_leaders as $leader_hash){
			
			$player_ids = array_keys($leader_hash);
			$count      = 0;
			foreach($player_ids as $player_id){
			  if(!isset($result[$player_id])){
			
				$player           = $mplayer->get($player_id);
				$person           = $mperson->get($player->person_id);
			
				$player->fullname = $person->lastname.', '.$person->firstname;
			
				if($player->pic_id && $count<3){
				  $pic          = $mpic->get($player->pic_id);
				  if($pic){
					$pic->pic = ModPbnyWeeklyLeadersHelper::getFlickrImageUrl($pic->flickr_id, 'thumbnail');
					$player->pic  = $pic;
				  }
				}
			
				$result[$player_id] = $player;
			  }
			  $count++;
			}
			
		}
		
		return $result;
	}
	
	function getFlickrImageUrl($photo_id, $size = null)
    {
      if(empty($photo_id))
      return null;
      
	  #load api and key
	  $vars   = (object)parse_ini_file('sample.ini');
	  $flickr = new phpFlickr($vars->api_key, $vars->api_secret);
      $flickr->setToken($vars->api_token);
	  
	  # get info of pic
	  $result = $flickr->photos_getInfo($photo_id);
	  
	  #build photo url
	  return $flickr->buildPhotoURL($result['photo'], $size);
    }
	
## PRIVATE METHODS:
	private function subval_sort($a,$subkey) {
	
		if(empty($a))
			return array();
			
		foreach($a as $k=>$v) {
			$b[$k] = strtolower($v[$subkey]);
		}
		arsort($b);
	
		foreach($b as $key=>$val) {
			$c[$key] = $a[$key];
		}
	
		return $c;
	}
	
}
?>
